﻿using RiskMan.DataClass.Models.Gilevskiy;
using System;
using System.Text.RegularExpressions;

namespace RiskMan.Views.Gilevskiy
{
    /// <summary>
    /// Условия обработки данных и сроковых в цифровые
    /// </summary>
    internal class LogikConditionToDitgit
    {

        private MainWindow mainWindow = MainWindow.StaticLevel;
        private DataClass.Data data;

        public LogikConditionToDitgit()
        {
            data = mainWindow._dataProvider;
        }

        /// <summary>
        /// Распознавание текстовых условия
        /// </summary>
        /// <param name="robot"></param>
        /// <returns></returns>
        public string ConvertConditionFromStringToDitgit(RobotGilevskiyItem robot)
        {
            string conditionInitial = robot.Condition.ToUpper();
            var conditionDitgit = conditionInitial;
            foreach (var item in data.TimeFrameItems)
            {
                string pattern = item.Instrument.ToUpper() + "#" + item.TimeFrame + "," + @"\d+" + "#" + "(CLOSE|OPEN|HIGH|LOW|VOLUME)";
                Regex regex = new Regex(pattern);
                Match match = regex.Match(conditionInitial);

                var ddd = match.Value;


                if (match.Success)
                {
                    while (regex.Match(conditionDitgit).Success)
                    {
                        var tmpIndex = ddd.Replace(item.Instrument.ToUpper() + "#" + item.TimeFrame + ",", "").Replace("#", "");
                        if (tmpIndex.Contains("HIGH"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("HIGH", ""))].High
                                    .ToString());
                        }
                        if (tmpIndex.Contains("LOW"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("LOW", ""))].Low
                                    .ToString());
                        }
                        if (tmpIndex.Contains("OPEN"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("OPEN", ""))].Open
                                    .ToString());
                        }
                        if (tmpIndex.Contains("CLOSE"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("CLOSE", ""))]
                                    .Close.ToString());
                        }
                        if (tmpIndex.Contains("VOLUME"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("VOLUME", ""))]
                                    .Volume.ToString());
                        }
                        ddd = regex.Match(conditionDitgit).Value;
                    }

                    if (conditionDitgit.Contains(item.Instrument.ToUpper() + "#PRICE#"))
                        conditionDitgit = conditionDitgit.Replace(item.Instrument.ToUpper() + "#PRICE#",
                          mainWindow._dataProvider.GetLastPrice(item.Instrument).ToString());
                }

            }


            return conditionDitgit;
        }

        public string ConvertConditionFromStringToDitgit(string condition, string instrument)
        {
            string conditionInitial = condition.ToUpper();
            var conditionDitgit = conditionInitial;
            foreach (var item in data.TimeFrameItems)
            {
                string pattern = item.Instrument.ToUpper() + "#" + item.TimeFrame + "," + @"\d+" + "#" + "(CLOSE|OPEN|HIGH|LOW|VOLUME)";
                Regex regex = new Regex(pattern);
                Match match = regex.Match(conditionInitial);

                var ddd = match.Value;


                if (match.Success)
                {
                    while (regex.Match(conditionDitgit).Success)
                    {
                        var tmpIndex = ddd.Replace(item.Instrument.ToUpper() + "#" + item.TimeFrame + ",", "").Replace("#", "");
                        if (tmpIndex.Contains("HIGH"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("HIGH", ""))].High
                                    .ToString());
                        }
                        if (tmpIndex.Contains("LOW"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("LOW", ""))].Low
                                    .ToString());
                        }
                        if (tmpIndex.Contains("OPEN"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("OPEN", ""))].Open
                                    .ToString());
                        }
                        if (tmpIndex.Contains("CLOSE"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("CLOSE", ""))]
                                    .Close.ToString());
                        }
                        if (tmpIndex.Contains("VOLUME"))
                        {
                            conditionDitgit = conditionDitgit.Replace(ddd,
                                item.CandleItems[item.CandleItems.Count - Convert.ToInt32(tmpIndex.Replace("VOLUME", ""))]
                                    .Volume.ToString());
                        }
                        ddd = regex.Match(conditionDitgit).Value;
                    }

                    if (conditionDitgit.Contains(item.Instrument.ToUpper() + "#PRICE#"))
                        conditionDitgit = conditionDitgit.Replace(item.Instrument.ToUpper() + "#PRICE#",
                          mainWindow._dataProvider.GetLastPrice(item.Instrument).ToString());
                }

            }


            return conditionDitgit;
        }
    }
}
